import { UserProfile } from './user'

// 团队角色枚举
export enum TeamRole {
  OWNER = 'OWNER',
  ADMIN = 'ADMIN',
  MEMBER = 'MEMBER'
}

// 团队成员接口
export interface TeamMember {
  id: string;
  userId: string;
  teamId: string;
  role: TeamRole;
  user: UserProfile;
}

// 团队变更类型枚举
export enum TeamChangeType {
  MEMBER_ADDED = 'MEMBER_ADDED',
  MEMBER_REMOVED = 'MEMBER_REMOVED',
  ROLE_CHANGED = 'ROLE_CHANGED',
  TEAM_CREATED = 'TEAM_CREATED',
  TEAM_UPDATED = 'TEAM_UPDATED',
  TEAM_DELETED = 'TEAM_DELETED'
}

// 团队活动日志接口
export interface TeamActivity {
  id: string;
  teamId: string;
  userId: string;
  type: TeamChangeType;
  details?: string;
  targetId?: string;
  targetName?: string;
  timestamp: Date;
}

// 用户在项目中的角色枚举
export enum ProjectRole {
  OWNER = 'OWNER',
  ADMIN = 'ADMIN',
  MEMBER = 'MEMBER',
  VIEWER = 'VIEWER'
}

// 角色权限级别
export const RolePermissionLevel = {
  [ProjectRole.OWNER]: 4,
  [ProjectRole.ADMIN]: 3,
  [ProjectRole.MEMBER]: 2,
  [ProjectRole.VIEWER]: 1
}

// 项目成员类型
export interface ProjectMember {
  id: string
  userId: string
  projectId: string
  role: ProjectRole
  joinedAt: Date
  user: UserProfile
}

// 项目邀请类型
export interface ProjectInvite {
  id: string
  email: string
  projectId: string
  role: ProjectRole
  status: 'PENDING' | 'ACCEPTED' | 'DECLINED'
  token: string
  createdAt: Date
  expiresAt: Date
}

// 项目邀请请求类型
export interface InviteUserRequest {
  email: string
  projectId: string
  role: ProjectRole
}

// 接受邀请请求类型
export interface AcceptInviteRequest {
  token: string
}

// 更新成员角色请求类型
export interface UpdateMemberRoleRequest {
  memberId: string
  role: ProjectRole
}

// 移除成员请求类型
export interface RemoveMemberRequest {
  memberId: string
}

// 检查用户是否有权限执行某操作的函数类型
export type PermissionChecker = (userRole: ProjectRole, targetRole?: ProjectRole) => boolean

// 常用权限检查函数
export const canManageMembers: PermissionChecker = (userRole: ProjectRole, targetRole?: ProjectRole) => {
  if (!targetRole) {
    // 检查是否有管理成员的权限
    return [ProjectRole.OWNER, ProjectRole.ADMIN].includes(userRole)
  }
  
  // 检查是否有权限管理特定角色的成员
  const userLevel = RolePermissionLevel[userRole]
  const targetLevel = RolePermissionLevel[targetRole]
  
  return userLevel > targetLevel
} 